#include<vector>
struct TreeNode
{
    int val;
    TreeNode* left;
    TreeNode* right;

    TreeNode() : val(0), left(nullptr), right(nullptr)
    {
    }

    TreeNode(int x) : val(x), left(nullptr), right(nullptr)
    {
    }

    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right)
    {
    }
};

class Solution {
public:
    int aim = 0;
std::vector<std::vector<int>> res;
    std::vector<int> level;
    std::vector<std::vector<int>> pathSum(TreeNode* root, int targetSum) {
        aim = targetSum;
       if (root == nullptr) return res;
        level.push_back(root->val);
        
        f(root,0);
        return res;
    }
    void f(TreeNode* root, int sum)
    {
        if (root->left == nullptr && root->right == nullptr)
        {
            if (sum+root->val == aim)
            {
                const std::vector<int> s(level);
                res.push_back(s);
            }
            level.pop_back();
            return;
        }
        if (root->left != nullptr)
        {
            level.push_back(root->left->val);
            f(root->left, sum+root->val);
        }
        if (root->right != nullptr)
        {
            level.push_back(root->right->val);
            f(root->right, sum+root->val);
        }
        level.pop_back();
    }
};
